package com.jader.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.jader.activities.LoginActivity;
import com.jader.beans.User;
import com.jader.db.MyData;
import com.jader.myviews.MyProgressDialog;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.loopj.android.http.PersistentCookieStore;
import com.zlp.jader.R;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewDebug.FlagToString;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class JaderApplication extends Application {
	private static String APPKEY = "b7e5e9007b0c";
	private static String APPSECRRET = "23278e53bcbb23cf01a790266f32448f";
	private static String IP = "http://10.204.1.18:8080";
	private static RequestQueue queue = null;
	private static BitmapUtils bitmapUtils;
	public static final int CANCEL_COLLECTID_SELLER = 0;
	public static final int ALL_GOODS = 1;
	public static final int GOODS_DETAIL = 2;
	public static final int SEARCH_GOODS = 3;
	public static final int SHOW_COMMENT = 4;
	public static final int SELLER_DETAIL = 5;
	public static final int SELLER_GOODSBYID = 6;
	public static final int COLLECTID_GOODS = 7;
	public static final int CANCEL_COLLECTID_GOODS = 8;
	public static final int COLLECTID_SELLER = 9;
	public static final int SEARCH_NUMBER = 10;
	// 查找图片轮播图片
	public static final int SEARCH_PICTURE = 11;
	public static final int INSERT_USER = 21;
	public static final int USER_CHANGEPASSWORD = 22;
	public static final int USER_LOGIN = 23;
	public static final int CHECK_PHONE = 24;
	public static final int ADDRESS_INSERT = 26;
	public static final int ADDRESS_DELETE = 27;
	public static final int ADDRESS_UPDATE = 28;
	public static final int ADDRESS_SELECT = 29;
	public static final int SELECT_TIEZI_BY_TIEZIID = 30;
	public static final int DELETE_SHOP_CART_GOODS = 31;
	public static final int SELECT_WEI_DU_XIAOXI = 32;
	public static final int UPDATE_XIAOXI_YIDU = 33;
	public static final int DELETE_XIAOXI_YIDU = 36;
	public static final int SELECT_USERBYID = 40;
	public static final int SELECT_ADDRESSBYUSERID = 41;
	public static final int INSERT_GOODS_ORDER_PAY = 42;
	public static final int INSERT_GOODS_TOSHOPCAT = 43;
	public static final int INSERT_GOODS_ORDER_CANCEL = 44;
	public static final int DELETE_GOODS_ORDER_CANCEL = 45;
	public static final int INSERT_TIEZI_HUIFU = 47;
	public static final int TIEZI_HUIFU = 46;
	public static final int INSERT_EVALUTE = 48;
	public static final int INSERT_ORDER_FROM_SHOPCART = 49;
	public static final int COLLECT_STORE_SELECT = 61;
	public static final int COLLECT_STORE_DELETE = 62;
	public static final int SHOPCART_SELECT = 63;
	public static final int SHOPCART_DELETE = 64;
	public static final int COLLECT_GOODS_SELECT = 65;
	public static final int COLLECT_GOODS_DELETE = 66;
	public static final int UPDATE_USERNAME = 67;
	public static final int UPDATE_USERPIC = 68;
	public static final int UPDATE_USERGENDER = 69;
	public static final int UPDATE_USERPHONE = 70;
	public static final int ORDER_SELECT = 71;
	public static final int MY_WALLET = 72;
	public static User user;
	public static HttpUtils httpclient;
	private ACache mCache;
	public static HttpUtils httpUtils;
	public static SQLiteDatabase sqLiteDatabase;
	private static MyData myData;
	public static ImageLoader imageLoader;
	public static Gson gson;
	public static int LOGIN_REQUESTCODE = 1;
	public static final int DIANZAN_REQUESTCODE = 2;

	@Override
	public void onCreate() {
		queue = Volley.newRequestQueue(getApplicationContext());
		imageLoader = new ImageLoader(queue, LruImageCache.instance());
		bitmapUtils = new BitmapUtils(getApplicationContext(), null, 0.5f);
		bitmapUtils.configDefaultLoadingImage(R.drawable.pic_loading);
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.pic_loading_fail);
		bitmapUtils.configDiskCacheEnabled(true);
		bitmapUtils.configMemoryCacheEnabled(true);
		httpUtils = new HttpUtils();
		httpUtils.configCurrentHttpCacheExpiry(60 * 60 * 24 * 1000);
		myData = new MyData(getBaseContext());
		mCache = ACache.get(this);
		httpclient = new HttpUtils();
		// PreferencesCookieStore myCookieStore =new
		// PreferencesCookieStore(this);
		PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
		httpclient.configCookieStore(myCookieStore);
		gson = new Gson();
		super.onCreate();
	}

	public RequestQueue getQueue() {
		if (queue == null) {
			queue = Volley.newRequestQueue(getApplicationContext());
		}
		return queue;
	}

	public static String getIP() {
		return IP;
	}

	public static void show(Context context, CharSequence text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	public static BitmapUtils getBitmapUtils() {
		
		return bitmapUtils;
	}

	public static void setListViewHeightBasedOnChildren(ListView listView) {
		if (listView == null)
			return;
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	public static String getAPPKEY() {
		return APPKEY;
	}

	public static String getAPPSECRRET() {
		return APPSECRRET;
	}

	public static String getPrice(double price) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(price);
	}

	public static boolean isEmpty(String string) {
		boolean flag = false;
		if (null == string || "".equals(string)) {
			flag = true;
		}
		return flag;
	}

	public static MyProgressDialog getMyDialog(Context context) {
		MyProgressDialog dialog = new MyProgressDialog(context, "",
				R.anim.loading);
		return dialog;

	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
		} else {
			// 如果仅仅是用来判断网络连接
			// 则可以使用 cm.getActiveNetworkInfo().isAvailable();
			NetworkInfo[] info = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static SQLiteDatabase getSqLiteDatabase() {
		return myData.getReadableDatabase();
	}

	public static String getCurProcessName(Context context) {

		int pid = android.os.Process.myPid();

		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);

		for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
				.getRunningAppProcesses()) {

			if (appProcess.pid == pid) {
				return appProcess.processName;
			}
		}
		return null;
	}

	public static void startShareApp(Context context, String sztitle,
			String title, String msg) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, title);
		intent.putExtra(Intent.EXTRA_TEXT, msg);
		context.startActivity(Intent.createChooser(intent, sztitle));

	}

	public static void login(Activity context) {
		show(context, "您现在还没有登录账户哦");
		Intent intent = new Intent(context, LoginActivity.class);
		context.startActivityForResult(intent, LOGIN_REQUESTCODE);
	}

	public static String getDate() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	static Activity activity;
	static Intent intent;
	static boolean isStart = false;

	public static void startService(Activity activity, Intent intent) {
		JaderApplication.activity = activity;
		JaderApplication.intent = intent;
		activity.startActivity(intent);
		isStart = true;

	}

	public static void stopService() {
		activity.stopService(intent);
		isStart = false;
	}

	public static boolean isStart() {
		return isStart;
	}

}
